package cn.jianyongjie.org.service.impl;

import cn.jianyongjie.basic.service.impl.BaseServiceImpl;
import cn.jianyongjie.basic.utils.MD5Utils;
import cn.jianyongjie.basic.utils.StrUtils;
import cn.jianyongjie.org.domain.Employee;
import cn.jianyongjie.org.mapper.EmployeeMapper;
import cn.jianyongjie.org.service.IEmployeeService;
import cn.jianyongjie.user.domain.Logininfo;
import cn.jianyongjie.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.logging.LogManager;

/**
* @Description: service层的实现类
* @Author: Jian Yongjie
* @Version: 1.0
* @Date:  2022/4/19 18:46
*/
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;


    @Override
    public List<Employee> findEmpById(Long id) {
        return employeeMapper.findEmpById(id);
    }


    /*
    * @Title: EmployeeServiceImpl
    * @Description: 重写新增方法
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/9 18:20
    * @Parameters: * @param employee
    * @Return void
    */
    @Override
    public void save(Employee employee){
        initEmployee(employee);
        Logininfo logininfo = employeeLogininfo(employee);
        // 新增logininfo
        logininfoMapper.save(logininfo);
        // 将logininfo装进员工表
        System.out.println(logininfo.getId());
        employee.setLogininfo_id(logininfo.getId());
        // 新增员工
        employeeMapper.saves(employee);
    }

    private Logininfo employeeLogininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        // 拷贝员工信息
        BeanUtils.copyProperties(employee,logininfo);
        // 设置管理员
        logininfo.setType(0);
        return logininfo;
    }

    private void initEmployee(Employee employee) {

        // 设置盐值
        String salt = StrUtils.getComplexRandomString(32);
        //2 通过这个盐值加密密码
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword()+salt);
        // 保存盐值
        employee.setSalt(salt);
        // 保存加密过后的密码
        employee.setPassword(md5Pwd);
    }

}
